package com.bob.light

import android.app.ActivityManager
import android.content.Context
import android.os.Process
import androidx.annotation.Nullable

object Tools {

    /**
     * 包名判断是否为主进程
     */
    @JvmStatic
    fun isMainProcess(context: Context?) : Boolean{
        if (context == null) {
            return false
        }
        return context.applicationContext.packageName == getCurrentProcessName(context)
    }

    private fun getCurrentProcessName(context: Context): String {
        val pid = Process.myPid()
        var currentProcessName = ""
        val activityManager = context.applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        activityManager.runningAppProcesses.forEach {
            if (it.pid == pid) {
                currentProcessName = it.processName
            }
        }
        return currentProcessName
    }

    @JvmStatic
    fun <T> requireNotNull(reference: T, @Nullable error: String): T {
        require(reference != null) {
            error
        }
        return reference
    }

}